package com.siwei.mes.service.sync.erp.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.sync.erp.ErpSupplierCompanyMaterials;
import com.siwei.mes.mapper.syncErp.ErpSupplierCompanyMaterialsMapper;
import com.siwei.mes.service.sync.erp.ErpSupplierCompanyMaterialsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * ERP-供应商原材料(ErpSupplierCompanyMaterials)表服务实现类
 *
 * @author linzi
 * @since 2024-08-17 09:51:17
 */
@Service
public class ErpSupplierCompanyMaterialsServiceImpl implements ErpSupplierCompanyMaterialsService {
    @Resource
    private ErpSupplierCompanyMaterialsMapper erpSupplierCompanyMaterialsMapper;

    @Override
    public Long create(ErpSupplierCompanyMaterials entity) {
        erpSupplierCompanyMaterialsMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ErpSupplierCompanyMaterials entity) {
        ErpSupplierCompanyMaterials erpSupplierCompanyMaterials = erpSupplierCompanyMaterialsMapper.selectByPrimaryKey(entity.getId());
        assertTrue(erpSupplierCompanyMaterials == null, "ERP-供应商原材料信息不存在");
        erpSupplierCompanyMaterialsMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ErpSupplierCompanyMaterials erpSupplierCompanyMaterials = erpSupplierCompanyMaterialsMapper.selectByPrimaryKey(id);
        assertTrue(erpSupplierCompanyMaterials == null, "ERP-供应商原材料信息不存在");
        erpSupplierCompanyMaterialsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ErpSupplierCompanyMaterials> getPage(PageQuery<ErpSupplierCompanyMaterials, ErpSupplierCompanyMaterials> query) {
        ErpSupplierCompanyMaterials searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> erpSupplierCompanyMaterialsMapper.getList(searchParam));
    }

    @Override
    public ErpSupplierCompanyMaterials loadById(Long id) {
        return erpSupplierCompanyMaterialsMapper.loadById(id);
    }

    @Override
    public List<ErpSupplierCompanyMaterials> getAll(ErpSupplierCompanyMaterials entity) {
        return erpSupplierCompanyMaterialsMapper.getList(entity);
    }

    @Override
    public void syncSaveOrUpdate(ErpSupplierCompanyMaterials entity) {
        Integer cyrid = entity.getCyrid();
        if (cyrid == null) {
            return;
        }
        /**判断是否存在*/
        ErpSupplierCompanyMaterials isExist = erpSupplierCompanyMaterialsMapper.selectByCyrId(cyrid);
        if (isExist == null) {
            entity.setCreateTime(new Date()).setUpdateTime(new Date()).setIsDel(0);
            erpSupplierCompanyMaterialsMapper.insertSelective(entity);
        } else {
            entity.setId(isExist.getId()).setUpdateTime(new Date());
            erpSupplierCompanyMaterialsMapper.updateByPrimaryKeySelective(entity);
        }
    }
}
